package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import com.itheima.service.AdressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = AdressService.class)
@Transactional
public class AdressServiceImpl implements AdressService{
    @Autowired
    private AddressDao addressDao;

    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();

        PageHelper.startPage(currentPage,pageSize);
        Page<Address> page = addressDao.findAll();
        long total = page.getTotal();
        List<Address> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public List<String> findAdressName() {
        List<Address>list = addressDao.findAdressName();
        List<String> nameList=new ArrayList<>();
        for (Address adress : list) {
            String name = adress.getName();
            nameList.add(name);
        }
        return nameList;
    }

    @Override
    public void add(Address address) {
        addressDao.add(address);
    }

    @Override
    public List<String> findAddressName() {
        List<Address> adressName = addressDao.findAdressName();
        List<String>list = new ArrayList<>();
        for (Address address : adressName) {
            list.add( address.getName());
        }
        return list;
    }

    @Override
    public PageResult findByString(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();  //过滤查询的条件

        PageHelper.startPage(currentPage,pageSize);

        Page<Address> page = addressDao.selectByCondition(queryString);

        long total = page.getTotal();

        List<Address> rows = page.getResult();
        PageResult pageResult = new PageResult(total,rows);
        System.out.println("-----------------------------------");
        System.out.println("-----------------------------------");
        System.out.println(pageResult.getRows());
        return pageResult;
    }


    public void deleteById(Integer id) {
        addressDao.deleteById(id);

    }

    @Override
    public List<Address> findAllll() {
        return addressDao.findAdressName();
    }
}
